﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace ClassProxitator.Messages
{
    public class ResultMessage : Message
    {
        public int TaskId { get; set; }
        public object Data { get; set; }

        public override MessageType MessageType
        {
            get { return MessageType.Result; }
        }

        public override async Task<byte[]> GetContentBytes()
        {
            using (var s = new MemoryStream())
            {
                await s.WriteAsync(this.TaskId).ConfigureAwait(false);
                var bf = new BinaryFormatter();
                bf.Serialize(s, new Box(this.Data));
                return s.ToArray();
            }
        }

        public override async Task LoadIn(Stream s)
        {
            this.TaskId = await s.ReadInt32Async().ConfigureAwait(false);
            var bf = new BinaryFormatter();
            this.Data = ((Box)bf.Deserialize(s)).Data;
        }
    }
}
